﻿using Aptea.OpenXmlOffice;
using DocumentFormat.OpenXml.Spreadsheet;
using eCRAM.Model;

namespace eCRAM.Bll
{
    /// <summary>
    /// Classe générant le pied de page de l'onglet contenant le tableau de détails des imputations d'un cram
    /// </summary>
    public class ExcelSheetFooterGenerator
    {
        /// <summary>
        /// Instance du manager Excel
        /// </summary>
        public ExcelManager ExcelManager { get; set; }

        /// <summary>
        /// Index de la ligne en cours d'insertion dans Excel
        /// </summary>
        public uint IdxRow { get; set; }

        /// <summary>
        /// Instance de l'onglet
        /// </summary>
        public Worksheet WorkSheet { get; set; }

        /// <summary>
        /// Cram contenant les données à exporter
        /// </summary>
        public Cram Cram { get; set; }

        /// <summary>
        /// Lance l'insertion du pied de page
        /// </summary>
        /// <returns></returns>
        public uint Generate()
        {
            IdxRow++;
            
            // On écrit la zone des visas
            AddVisas();
            IdxRow++;

            // On écrit la section de controle administratif
            //AddAdministrativeControl();
            IdxRow++;


            // On écrit la section de copyright
            AddCopyright();

            return IdxRow;
        }

        /// <summary>
        /// Ajoute la section contenant le copyright
        /// </summary>
        private void AddCopyright()
        {
            ExcelManager.AddFooter(WorkSheet, "&L&\"Comic Sans MS\"AA/ADM/0001&C&\"Comic Sans MS\"© APTEA&R&\"Comic Sans MS\"Tous droits réservés");

            //var cell = ExcelManager.AddDataToSheet(WorkSheet, IdxRow, 2, "AA/ADM/0001");
            //ExcelManager.AddStyleToCell(cell, CramExcelStyle.CopyrightStyle);

            //cell = ExcelManager.AddDataToSheet(WorkSheet, IdxRow, 16, "© APTEA");
            //ExcelManager.AddStyleToCell(cell, CramExcelStyle.CopyrightStyle);

            //cell = ExcelManager.AddDataToSheet(WorkSheet, IdxRow, 27, "Tous droits réservés");
            //ExcelManager.AddStyleToCell(cell, CramExcelStyle.CopyrightStyle);

            //IdxRow++;
        }

        /// <summary>
        /// Ajoute la section contenant la zone de controle administratif
        /// </summary>
        private void AddAdministrativeControl()
        {
            var cell = ExcelManager.AddDataToSheet(WorkSheet, IdxRow, 2, "Contrôle administratif");
            ExcelManager.AddStyleToCell(cell, CramExcelStyle.DefaultBorderStyle);
            WorkSheet.GetMergeCells(IdxRow, 2, IdxRow, 31);
            ExcelManager.AddStyleToCells(WorkSheet, IdxRow, 2, IdxRow, 31, CramExcelStyle.FooterStyle);

            IdxRow++;
            ExcelManager.AddStyleToCells(WorkSheet, IdxRow, 2, IdxRow, 1, CramExcelStyle.FooterStyle);

            WorkSheet.GetMergeCells(IdxRow, 2, IdxRow, 31);
            cell = ExcelManager.AddDataToSheet(WorkSheet, IdxRow, 2, "Conforme                                 Non conforme                                   Contrôlé par                             Le");
            ExcelManager.AddStyleToCell(cell, CramExcelStyle.DefaultBorderStyle);


            ExcelManager.AddStyleToCells(WorkSheet, IdxRow, 2, IdxRow, 31, CramExcelStyle.FooterStyle);

            IdxRow++;


            ExcelManager.AddStyleToCells(WorkSheet, IdxRow, 2, IdxRow, 2, CramExcelStyle.FooterStyle);

            WorkSheet.GetMergeCells(IdxRow, 2, IdxRow, 31);
            ExcelManager.AddStyleToCells(WorkSheet, IdxRow, 2, IdxRow, 31, CramExcelStyle.FooterStyle);
        }


        /// <summary>
        /// Ajoute la section contenant la zone de visas
        /// </summary>
        private void AddVisas()
        {
            const uint indexVisas = 5;
            const uint largeurZone = 7;
            const uint indexClient = indexVisas + largeurZone + 1;
            const uint idxResponsable = indexClient + largeurZone + 1;

            

            // label visas
            WorkSheet.GetMergeCells(IdxRow, indexVisas, IdxRow, idxResponsable + largeurZone);
            ExcelManager.AddDataToSheet(WorkSheet, IdxRow, indexVisas, "Visas");
            ExcelManager.AddStyleToCells(WorkSheet, IdxRow, indexVisas, IdxRow, idxResponsable + largeurZone,
                CramExcelStyle.GetDefaultCellSyleWithThickBorders(EnumBordure.Left | EnumBordure.Right | EnumBordure.Top));
            IdxRow++;

          

            // label collaborateur
            WorkSheet.GetMergeCells(IdxRow, indexVisas, IdxRow, indexVisas + largeurZone);
            WorkSheet.GetMergeCells(IdxRow + 1, indexVisas, IdxRow + 3, indexVisas + largeurZone);
            ExcelManager.AddDataToSheet(WorkSheet, IdxRow, indexVisas, "Collaborateur");
            ExcelManager.AddStyleToCells(WorkSheet, IdxRow, indexVisas, IdxRow, indexVisas + largeurZone,
                CramExcelStyle.GetDefaultCellSyleWithThickBorders(EnumBordure.Left ));
            ExcelManager.AddStyleToCells(WorkSheet, IdxRow + 1, indexVisas, IdxRow + 3, indexVisas + largeurZone,
                CramExcelStyle.GetDefaultCellSyleWithThickBorders(EnumBordure.Left | EnumBordure.Bottom));

            // label Client
            WorkSheet.GetMergeCells(IdxRow, indexClient, IdxRow, indexClient + largeurZone);
            WorkSheet.GetMergeCells(IdxRow + 1, indexClient, IdxRow + 3, indexClient + largeurZone);
            ExcelManager.AddDataToSheet(WorkSheet, IdxRow, indexClient, "Client");
            ExcelManager.AddStyleToCells(WorkSheet, IdxRow, indexClient, IdxRow, indexClient + largeurZone, CramExcelStyle.GetDefaultCellSyleWithThickBorders(EnumBordure.None));
            ExcelManager.AddStyleToCells(WorkSheet, IdxRow + 1, indexClient, IdxRow + 3, indexClient + largeurZone, 
                CramExcelStyle.GetDefaultCellSyleWithThickBorders(EnumBordure.Bottom));

            // label Responsable Aptea
            WorkSheet.GetMergeCells(IdxRow, idxResponsable, IdxRow, idxResponsable + largeurZone);
            WorkSheet.GetMergeCells(IdxRow + 1, idxResponsable, IdxRow + 3, idxResponsable + largeurZone);
            ExcelManager.AddDataToSheet(WorkSheet, IdxRow, idxResponsable, "Responsable APTEA");
            ExcelManager.AddStyleToCells(WorkSheet, IdxRow, idxResponsable, IdxRow, idxResponsable + largeurZone, CramExcelStyle.GetDefaultCellSyleWithThickBorders(EnumBordure.Right));
            ExcelManager.AddStyleToCells(WorkSheet, IdxRow + 1, idxResponsable, IdxRow + 3, idxResponsable + largeurZone,
                CramExcelStyle.GetDefaultCellSyleWithThickBorders(EnumBordure.Right | EnumBordure.Bottom));

            IdxRow += 4;
        }

    }
}
